<?php


use Yaf\Session;
use Yaf\Registry;
use models\Business\Address;
use models\Business\Member;


class AddressController extends ApiBaseController{

    /**
     * @SWG\Get(
     *     path="/admin/address/list",
     *     tags={"收货地址"},
     *     summary="获取收货地址列表数据接口",
     *     @SWG\Parameter(
     *          name="Authorization",
     *          description="Authorization 登录接口返回的jwt字段的值",
     *          in="header",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="page",
     *          description="页码",
     *          in="query",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="page_size",
     *          description="每页显示条数",
     *          in="query",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="sort",
     *          description="排序方式 可选值: desc|asc",
     *          in="query",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="order",
     *          description="排序字段 可选值: id",
     *          in="query",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="mobile",
     *          description="完整会员登陆手机号",
     *          in="query",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="sign",
     *          description="签名",
     *          in="query",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Response(
     *         response="200",
     *         description="请求成功",
     *         @SWG\Schema(type="object", ref="#/definitions/addressListData")
     *     ),
     *     @SWG\Response(
     *         response="400",
     *         description="签名不存在或无效签名"
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="jwt无效或过期，需要登录"
     *     ),
     *     @SWG\Response(
     *         response="403",
     *         description="无权访问"
     *     ),
     *     @SWG\Response(
     *         response="404",
     *         description="找不到数据"
     *     )
     * )
     */
    public function listAction() {
        $parameters = Registry::get('parameters');

        if (isset($parameters['mobile']))
            $parameters['member_id'] = Member::getMemberIdByMobile($parameters['mobile']);

        $addresses = Address::getAddressList($parameters);

        return $this->_responseJson($addresses);
    }


    /**
     * @SWG\Put(
     *     path="/admin/address/{id}/edit",
     *     tags={"收货地址"},
     *     summary="编辑收货地址接口",
     *     description="",
     *     @SWG\Parameter(
     *          name="Authorization",
     *          description="Authorization 登录接口返回的jwt字段的值",
     *          in="header",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="id",
     *          description="/admin/Address/list接口返回的id字段的值",
     *          in="path",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="member_id",
     *          description="所属会员",
     *          in="formData",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="is_default",
     *          description="是否默认地址(0为否,1为是,默认1)",
     *          in="formData",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="province",
     *          description="省份",
     *          in="formData",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="city",
     *          description="城市",
     *          in="formData",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="area",
     *          description="区县",
     *          in="formData",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="street",
     *          description="街道",
     *          in="formData",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="location",
     *          description="详细地址",
     *          in="formData",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="name",
     *          description="收货人姓名",
     *          in="formData",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="zipcode",
     *          description="邮编",
     *          in="formData",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="mobile",
     *          description="收货人手机号",
     *          in="formData",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="sign",
     *          description="签名",
     *          in="formData",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Response(
     *         response="200",
     *         description="请求成功",
     *         @SWG\Schema(type="object", ref="#/definitions/addressSingleData")
     *     ),
     *     @SWG\Response(
     *         response="422",
     *         description="编辑失败"
     *     ),
     *     @SWG\Response(
     *         response="400",
     *         description="签名不存在或无效签名"
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="jwt无效或过期，需要登录"
     *     ),
     *     @SWG\Response(
     *         response="403",
     *         description="无权访问"
     *     ),
     *     @SWG\Response(
     *         response="404",
     *         description="找不到数据"
     *     )
     * )
     */
    public function editAction() {
        $parameters = Registry::get('parameters');

        $address = Address::editAddress($parameters);

        return $this->_responseJson(['data' => $address]);
    }
}
